package controller

import (
	"net/http"

	"example.com/m/demo14/models"
	"github.com/gin-gonic/gin"
)

type Student struct{}

func (con *Student) Create(c *gin.Context) {
	stu := models.Student{Name: "张三", Age: 21, Gender: "男"}
	models.DB.Create(&stu)
	c.JSON(http.StatusOK, gin.H{"status": "ok", "data": stu})
}

func (con *Student) GetStuList(c *gin.Context) {
	var stuList []models.Student
	models.DB.Where("age>?", 20).Find(&stuList)
	c.JSON(http.StatusOK, gin.H{"status": "ok", "data": stuList})
}

func (con *Student) Update(c *gin.Context) {
	updateList := models.DB.Where("gender=?", "男").Find(&[]models.Student{}).Update("gender", "女")
	c.JSON(http.StatusOK, gin.H{"status": "ok", "data": updateList})
}

func (con *Student) GetStu(c *gin.Context) {
	var stuList []models.Student
	//id >2 且 <5 且 age = 22  gender = 男
	// models.DB.Where("id>2 AND id <5 AND age =22  AND gender=男").Find(&stuList)
	//id in 一个 切片中
	// models.DB.Where("id in (?)", []int{2, 4, 5}).Find(&stuList)
	//name  like 中有 小的
	// models.DB.Where("name like ?", "%小%").Find(&stuList)
	//查找 age为20 或 age 为 21的
	models.DB.Where("age=? Or age=?", 20, 21).Find(&stuList)

	c.JSON(http.StatusOK, gin.H{"status": "ok", "data": stuList})
}
